from globals import *
from racer import racer

class menu(DirectObject):#subclassing is necessary to accept events
    def __init__(self):
        #Call load models to load all players/environment
        self.loadModels()
        
        #create the racers
        self.pirate = racer("pirate",(0,0,0,0), True)
        self.knight = racer("knight",(0,0,0,0), True)
        self.viking = racer("viking",(0,0,0,0), True)
        self.caveman = racer("caveman",(0,0,0,0), True)
        
        
        #turn off mouse control
        base.disableMouse()
        
        render.setShaderAuto()
        #camera.setPosHpr(0,-15,7,0,-15,0)
        
        #create some awesome lighting
        self.setupLights()
        
        #set up music
        self.music = loader.loadMusic("sound/idle.wav")
        SoundInterval(self.music).loop()
        
    def disable(self):
        """this function needs to destroy every object in this class... forever."""
        self.pirate.racer.hide()
        self.knight.racer.hide()
        self.viking.racer.hide()
        self.caveman.racer.hide()
        self.env.hide()
        
    def start_game(self):
        return not noRacer
        
    def choose(self, task):
        min_x = self.pirate.racer.getX()
        x = self.pirate.racer.getX()
        y = self.pirate.racer.getY()
        base.camera.setPosHpr(-10.05,-8,6,0,0,0)
        #camera.setPos(-10.05,-7,1.5)
        base.camera.lookAt(self.pirate.racer)
        #select a car
        if keyMap["rightA"]:
            if( x != (min_x + 6)):
                x += 2
                camera.setPos(x,y,2)
        if keyMap["leftA"]:
            if( x != min_x):
                x -= 2
                camera.setPos(x,y,2)
        if keyMap["forwardA"]:
            if(x == self.pirate.racer.getX()):
                carA = "pirate"
            if(x == self.knight.racer.getX()):
                carA = "knight"
            if(x == self.viking.racer.getX()):
                carA = "viking"
            if(x == self.caveman.racer.getX()):
                carA = "caveman"
            numRacers += 1
        if keyMap["forwardB"]:
            if(x == self.pirate.racer.getX()):
                carB = "pirate"
            if(x == self.knight.racer.getX()):
                carB = "knight"
            if(x == self.viking.racer.getX()):
                carB = "viking"
            if(x == self.caveman.racer.getX()):
                carB = "caveman"
            numRacers += 1

        if keyMap["forwardC"]:
            if(x == self.pirate.racer.getX()):
                carC = "pirate"
            if(x == self.knight.racer.getX()):
                carC = "knight"
            if(x == self.viking.racer.getX()):
                carC = "viking"
            if(x == self.caveman.racer.getX()):
                carC = "caveman"
            numRacers += 1
                
        if keyMap["forwardD"]:
            if(x == self.pirate.racer.getX()):
                carD = "pirate"
            if(x == self.knight.racer.getX()):
                carD = "knight"
            if(x == self.viking.racer.getX()):
                carD = "viking"
            if(x == self.caveman.racer.getX()):
                carD = "caveman"
            numRacers += 1

        if keyMap["enter"]:
            if(numRacers >= 2):
                self.noRacer = False
        
    def loadModels(self):
        #first set up the environment
        self.env = loader.loadModel("models/track")
        self.env.reparentTo(render)
        self.env.setScale(5)
        #self.env.setPos(0,0,0)
        
    def setupLights(self):
        """Sets intitial lighting"""
        self.dirLight = DirectionalLight("dirLight")
        self.dirLight.setColor((.6,.6,.6,1))#Sets color of the directional light, alpha means nothing
        #create node path and connect directly to the scene
        self.dirLightNP = render.attachNewNode(self.dirLight) #paths to node
        self.dirLightNP.setHpr(0,-26,0) #sets light rotation
        #the nodepath that calls setLight is what gets illuminated by the light, will call on render
        render.setLight(self.dirLightNP)
        #use clearLight() to turn it off
        
        self.ambientLight = AmbientLight("ambientLight")
        self.ambientLight.setColor((.25,.25,.25,1))
        self.ambientLightNP = render.attachNewNode(self.ambientLight)
        render.setLight(self.ambientLightNP)   
        
        